package com.cndw.kungfu.model.activity.domain;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cndw.kungfu.config.ConstantsConfig;
import com.cndw.kungfu.ext.XmlProcesser;

public class CrossServer {

	private int serverId;
	private String svrId;
	private String name;
	private int classId;
	private String ip;
	private int port;
	private int type;

	public int getServerId() {
		return serverId;
	}

	public void setServerId(int serverId) {
		this.serverId = serverId;
	}

	public String getSvrId() {
		return svrId;
	}

	public void setSvrId(String svrId) {
		this.svrId = svrId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getClassId() {
		return classId;
	}

	public void setClassId(int classId) {
		this.classId = classId;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	private static final Map<Integer, CrossServer> dataMap = new HashMap<Integer, CrossServer>();

	private static final String configFile = "cross_server.xml";

	static {
		load();
	}

	public static synchronized void load() {
		if (dataMap.isEmpty()) {
			String fileName = ConstantsConfig.getLocaleResource() + configFile;
			List<CrossServer> dataList = XmlProcesser.processXml(CrossServer.class, fileName);
			if (dataList != null && dataList.size() > 0) {
				for (CrossServer e : dataList) {
					// TODO 这里要过滤不属于我的服务器
					int lid = ConstantsConfig.isLocalHost() ? 3 : 2;
					if (e.getClassId() != lid) {
						continue;
					}
					dataMap.put(e.getServerId(), e);
				}
			}
		}
	}

	public static synchronized void clear() {
		dataMap.clear();
		load();
	}

	public static Collection<CrossServer> getAll() {
		return dataMap.values();
	}

	public static CrossServer getOne(int id) {
		return dataMap.get(id);
	}

	public static int getArea(int serverId) {
		CrossServer server = getOne(serverId);
		return server == null ? 0 : server.getType();
	}

	public static int getArea(int sid, int defaultId) {
		int area = getArea(sid);
		return area == 0 ? defaultId : area;
	}
	
	public static int getMaxArea(){
		int max = 0;
		for (CrossServer e : getAll()) {
			if (e.getType() > max) {
				max = e.getType();
			}
		}
		return max;
	}

}
